package fr.fermeManager.application.utils;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 
 * @author Mo
 *
 */
public class BddConnexion {
	
	public static Connection getConnection() throws Exception{
		
		BddBackup.sauvegarderMois();
		
		String driver = Messages.getString(Constantes.BDD_DRIVER);
		
		Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
		
		Class.forName(driver).newInstance();
		
		String login = Messages.getString(Constantes.BDD_LOGIN);
		String password = Messages.getString(Constantes.BDD_PASS);
		String protocol = Messages.getString(Constantes.BDD_URL);
		
		Connection conn = DriverManager.getConnection(protocol + ";create=false;upgrade=false", login, password); //$NON-NLS-1$
		
		return conn;
	}
	
	public static void fermerBdd() throws Exception{
		try {
			DriverManager.getConnection("jdbc:derby:;shutdown=true");
		} catch (SQLException e) {
			if (((e.getErrorCode() == 50000)
			        && ("XJ015".equals(e.getSQLState())))) {
			}else{
				throw e;
			}
		}
	}
}
